//
//  JXHStatusViewModel.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/24.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit

class JXHStatusViewModel: NSObject {

    var status : JXHStatusModel?
    
    // MARK: - 对数据处理的属性
    
    var cellHeght : CGFloat  = 0
    
    var sourceText : String?     // 来源
    var created_atText : String? // 创建时间
    
    var verifiedImage : UIImage? // 认证图片
    var mbrankImage : UIImage?   // 会员等级图片
    var iconUrl : URL? // 用户头像URL
    var picURLs : [URL] = [URL].init() // 初始化，不是可选类型
    
    
    // MARK: - 自定义构造函数
    init(status : JXHStatusModel) {
        
        self.status = status
        
        if let source = status.source , source != "" {
            
            let startIndex = (source as NSString).range(of: ">").location + 1 // 位置
            let length = (source as NSString).range(of: "</").location - startIndex // 长度
            sourceText = (source as NSString).substring(with: NSRange.init(location: startIndex, length: length))
        }
        
        if let created_at = status.created_at  {
            created_atText = Date.dateSting(createAt: created_at)
        }
        
        let verified_type = status.user?.verified_type ?? -1
        
        switch verified_type {
        case 0:
            verifiedImage = UIImage.init(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage.init(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage.init(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        let mbrank = status.user?.mbrank ?? 0
        
        if mbrank>0 && mbrank<=6 {
            mbrankImage = UIImage.init(named: "common_icon_membership_level\(mbrank)")
        }
        
        let iconString  = status.user?.profile_image_url ?? ""
        
        iconUrl = URL.init(string: iconString)
        
        let picDics = status.pic_urls!.count != 0 ?status.pic_urls : status.retweeted_status?.pic_urls
        
        if let picDics = picDics {
            
            for picDic in picDics {
                
                guard let picString = picDic["thumbnail_pic"] else {
                    continue
                }
                picURLs.append(URL.init(string: picString)!)
            }
        }
    }
}

